package mn.slide.laborberlin.main;

import java.util.List;

import mn.slide.laborberlin.adaptor.LeiExtendableAdapter;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.model.Eintrag;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;

public class LeistungsverszeichnisActivity extends Activity {
	ExpandableListView list;
	int width;
	List<Eintrag> eintragList;
	LinearLayout az;

	EditText search_edt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.leistungsverszeichnis);
		View footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.leistun_content, null, false);

		list = (ExpandableListView) findViewById(R.id.expandableListView1);
		list.addHeaderView(footerView);
		list.setGroupIndicator(null);
		LeiExtendableAdapter adaptor = new LeiExtendableAdapter(this);
		list.setAdapter(adaptor);
		az = (LinearLayout) findViewById(R.id.az);
		// back_btn = (Button) findViewById(R.id.back_btn);
		// back_btn.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// onBackPressed();
		// }
		// });
		az.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final Intent intent = new Intent(
						LeistungsverszeichnisActivity.this,
						EintragActivity.class);
				intent.putExtra("position", -1);
				az.postDelayed(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						HomeGroupActivity.group.goTo(intent);

					}
				}, 300);

			}
		});
		for (int i = 0; i < adaptor.getGroupCount(); i++) {
			list.expandGroup(i);
		}
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					String k = search_edt.getText().toString();
					if (k.length() > 0) {

						Intent intent = new Intent(
								LeistungsverszeichnisActivity.this,
								SearchActivity.class);
						intent.putExtra("searchTxt", k);
						HomeGroupActivity.group.goTo(intent);

					}
					return true;
				}
				return false;
			}
		});

	}

	@Override
	public void onBackPressed() {

		GroupStarter.getInstance().onBackpressed(this);
	}
}
